package baseTest;

public class numIslands {
    public int process(char[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1'){
                    int a = area(grid,i,j);
                    res = Math.max(res,a);
                }
            }
        }
        return res;
    }

    public int area(char[][] grid,int r,int c){
        if (!inArea(grid,r,c)){
            return 0;
        }
        if (grid[r][c] != '1'){
            return 0;
        }
        grid[r][c] = '2';
        return 1+area(grid,r-1,c)
                +area(grid,r+1,c)
                +area(grid,r,c+1)
                +area(grid,r,c-1);
    }

    public boolean inArea(char[][] grid,int r,int c){
        return 0<=r && r< grid.length && 0<=c && c<grid[0].length;
    }
}
